#include "h_cc2640r2f_spi.h"

#include <ti/drivers/SPI.h>
#include "h_cc2640r2f_define.h"
#include <ti/devices/cc26x0r2/driverlib/ioc.h>

#include "h_cc2640r2f_uart.h"
//#include "h_cc2640r2f_gpio.h"

//#include <board.h>
////////////////////////////////////////////////////////////////////////////////
#define H_CC2640R2F_SPI0bitRate             1000000
#define H_CC2640R2F_SPI0dataSize            8
#define H_CC2640R2F_SPI0FrameFormat         SPI_POL1_PHA1


////////////////////////////////////////////////////////////////////////////////


static SPI_Handle      H_CC2640R2F_SPI0Handle = NULL;

signed char H_CC2640R2F_SPI0_Init(void)
{
    //signed char rslt = 0;
    SPI_Params H_CC2640R2F_SPIparams;
    
    SPI_init();
    SPI_Params_init(&H_CC2640R2F_SPIparams);
    H_CC2640R2F_SPIparams.bitRate = H_CC2640R2F_SPI0bitRate;
    H_CC2640R2F_SPIparams.custom = NULL;
    H_CC2640R2F_SPIparams.dataSize = H_CC2640R2F_SPI0dataSize;
    H_CC2640R2F_SPIparams.frameFormat = H_CC2640R2F_SPI0FrameFormat;
    H_CC2640R2F_SPIparams.mode = SPI_MASTER;
    H_CC2640R2F_SPIparams.transferCallbackFxn = NULL;
    H_CC2640R2F_SPIparams.transferMode = SPI_MODE_BLOCKING;
    H_CC2640R2F_SPIparams.transferTimeout = SPI_WAIT_FOREVER;
    
    H_CC2640R2F_GPIO_OutputSet(H_CC2640R2F_SPI0IO, H_CC2640R2F_SPIDisable);
    
    H_CC2640R2F_SPI0Handle = SPI_open(0, &H_CC2640R2F_SPIparams);
    
    if(NULL == H_CC2640R2F_SPI0Handle)
    {
        return (-1);
    }
    return 0;      
}

/*
void H_CC2640R2F_SPI0_CSEnable(void)
{
  H_CC2640R2F_GPIO_OutputSet(H_CC2640R2F_SPI0IO, H_CC2640R2F_SPIEnable);
}

void H_CC2640R2F_SPI0_CSDisable(void)
{
  H_CC2640R2F_GPIO_OutputSet(H_CC2640R2F_SPI0IO, H_CC2640R2F_SPIDisable);
}
*/

signed char H_CC2640R2F_SPI0_Trans(unsigned char *wdata, unsigned char *rdata, \
                                    unsigned short length)
{
    //signed char rslt = 0;
    
    SPI_Transaction spiTransaction;
    spiTransaction.arg = NULL;
    spiTransaction.count = length;
    spiTransaction.txBuf = (void *)wdata;
    spiTransaction.rxBuf = (void *)rdata;
    
    
    //rslt = SPI_control(H_CC2640R2F_SPIHandle, (SPI_CMD_RESERVED + 2), \
                        &cspin);
    SPI_transfer(H_CC2640R2F_SPI0Handle, &spiTransaction);
    //SPI_transferCancel(H_CC2640R2F_SPI0Handle);
    
    //SPI_close(H_CC2640R2F_SPIHandle);
    //H_CC2640R2F_UART_Debug("The spi status is %d\r\n", spiTransaction.status);
    return spiTransaction.status;
    
    //return rslt;
    
}

/*
#include "board.h"
#define MSGSIZE 10
void H_CC2640R2F_SPI_Test(void)
{
    
    
    
    signed char rslt = 0;
    
    unsigned char rdata[30];
    unsigned char wdata[30];
    
    //H_CC2640R2F_SPI0_Init();
    
    wdata[0] = 0x9F;
    H_CC2640R2F_SPI0_CSEnable();
    rslt = H_CC2640R2F_SPI0_Trans(wdata, rdata, 20);
    H_CC2640R2F_SPI0_CSDisable();
    
    H_CC2640R2F_UART_Debug("Manufacturer ID: 0x%02x\r\n", rdata[1]);
    H_CC2640R2F_UART_Debug("Memory Type: 0x%02x\r\n", rdata[2]);
    H_CC2640R2F_UART_Debug("Capacity: 0x%02x\r\n", rdata[3]);
    
    
    
    H_CC2640R2F_UART_Debug("The flash op rslt is %d\r\n", rslt);
    
    
    
    
    
}
*/
